//
//  SPWriteOrderInfoViewController.m
//  panzer
//
//  Created by 还带大道 on 2018/10/23.
//  Copyright © 2018年 还带大道. All rights reserved.
//

#import "SPWriteOrderInfoViewController.h"
#import "SPCommon.h"
#import "SPOrderDetailSectionHeaderView.h"
#import "SPWriteOrderInfoHeaderView.h"
#import "SPWriteOrderInfofooterView.h"
#import "SPWriteOrderInfoGoodsTableViewCell.h"



@interface SPWriteOrderInfoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,assign) NSInteger order_id;
@property (nonatomic,assign) SJOrderType type;
@property (nonatomic,strong) NSArray *expressList;
@property (nonatomic,strong) SPWriteOrderInfoHeaderView *headerV;

@end

@implementation SPWriteOrderInfoViewController

- (instancetype)initWithOrder_id:(NSInteger)order_id type:(SJOrderType)type
{
    if(self = [super init]){
        _order_id = order_id;
        _type = type;
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"填写运货单";
    self.submitBtn.layer.cornerRadius = 5;
    [self setupTableView];
    [self fetchExpressList];
   
}


- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.tableView.tableHeaderView.height = 120;
    self.tableView.tableFooterView.height = 190;
}



- (void)setupTableView
{
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    SPWriteOrderInfoHeaderView *headerV = [SPWriteOrderInfoHeaderView shareHeaderView];
    _headerV = headerV;
    headerV.backgroundColor = [UIColor whiteColor];
    self.tableView.tableHeaderView = headerV;
    
    SPWriteOrderInfofooterView *footerV = [SPWriteOrderInfofooterView shareHeaderView];
    footerV.backgroundColor = [UIColor whiteColor];
    self.tableView.tableFooterView = footerV;
    
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPWriteOrderInfoGoodsTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([SPWriteOrderInfoGoodsTableViewCell class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPOrderDetailSectionHeaderView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:NSStringFromClass([SPOrderDetailSectionHeaderView class])];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}


#pragma mark--
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 125;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SPOrderDetailSectionHeaderView *headerV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([SPOrderDetailSectionHeaderView class])];
    headerV.backgroundColor = [UIColor whiteColor];
    headerV.title.text = [NSString stringWithFormat:@"服务单号：18754899654"];
    headerV.des.text = @"换货";
    return headerV;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SPWriteOrderInfoGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SPWriteOrderInfoGoodsTableViewCell class])];
    return cell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

//提交
- (IBAction)submitBtnOnClick {
    NSDictionary *info;
    if(self.headerV.select_index == -1){
        [WToast showWithText:@"请选择快递公司"];
        return;
    }else{
        if(self.headerV.select_index < self.expressList.count){
            info = self.expressList[self.headerV.select_index];
        }
    }
    
    if(self.headerV.textFiled.text.length < 1){
        [WToast showWithText:@"请填写运单号"];
        return;
    }
    
    NSString *msg = [NSString stringWithFormat:@"%@\n%@",info[@"text"],self.headerV.textFiled.text];
    UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"确认运货单信息" message:msg preferredStyle:UIAlertControllerStyleAlert];
    
    [ac addAction:[UIAlertAction actionWithTitle:@"提交" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        /**
         //售后发货
         http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&ctrl=live_merch&ac=member&op=member&m=bz_activities&ta=post_expr&token=568e86c8630f7b44f29a23ba211d9f25
         参数  shouhou_id 售后id  express_id 物流公司id  express_no 订单号
         
         商家订单发货
         http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&ctrl=live_merch&ac=member&op=member&m=bz_activities&ta=express_post&token=568e86c8630f7b44f29a23ba211d9f25
         参数
         参数 order_id 订单id  express_id 快递公司列表id express_no 快递订单号
         
         */
        
        HelenParametersDic *dict = [HelenParametersDic h_dictionary];
        dict[@"ac"] = @"member";
        dict[@"op"] = @"member";
        dict[@"ta"] = @"post_expr";
        if(self.type == SJOrderType_DFH){
            dict[@"order_id"] = @(self.order_id);
        }else{
           dict[@"shouhou_id"] = @(self.order_id);
        }
        
        dict[@"express_id"] = info[@"id"];
        dict[@"express_no"] = self.headerV.textFiled.text;
        dict[@"token"] = [RZ_USERDEFAULTES objectForKey:BUSSION_INFO][@"token"];
        
        [HelenHttpClient requestWithUrlString:BASE_URL andParam:dict andMethod:@"POST" progress:nil success:^(id responseObject) {
            
            NSDictionary *res = responseObject[@"message"];
            NSInteger code = [res[@"resultCode"] integerValue];
            if(code== 0 || code ==1){
                [WToast showWithText:res[@"resultMessage"]];
            }else{
                [WToast showWithText:res[@"resultMessage"]];
            }
            
        } andFailBlock:^(NSInteger ErrorCode, NSError *ErrorString) {
            
        }];
        
    }]];
    
    
    [ac addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [self presentViewController:ac animated:YES completion:nil];
    
}





#pragma mark--快递列表
- (void)fetchExpressList
{
    /**
     http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&ctrl=live_merch&ac=member&op=member&m=bz_activities&ta=express&token=568e86c8630f7b44f29a23ba211d9f25
     
     */
    
    HelenParametersDic *dict = [HelenParametersDic h_dictionary];
    dict[@"ac"] = @"member";
    dict[@"op"] = @"member";
    dict[@"ta"] = @"express";
    dict[@"token"] = [RZ_USERDEFAULTES objectForKey:BUSSION_INFO][@"token"];
    
    [HelenHttpClient requestWithUrlString:BASE_URL andParam:dict andMethod:@"POST" progress:nil success:^(id responseObject) {
        
        NSDictionary *res = responseObject[@"message"];
        NSInteger code = [res[@"resultCode"] integerValue];
        if(code== 0 || code ==1){
            self.expressList = res[@"data"];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.headerV.expressList = self.expressList;
            });
        }
        
    } andFailBlock:^(NSInteger ErrorCode, NSError *ErrorString) {
        
    }];
}

@end
